package com.jiyun.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.User;
import com.jiyun.service.UserService;
import common.models.dtos.SmsDto;
import common.until.HttpUtils;
import common.until.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(User user) {
        if (user==null){
            return "参数有误";
        }
        String password = user.getPassword();
        String username = user.getUsername();
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User nameByuser = this.getOne(queryWrapper);
        if (StringUtils.isEmpty(nameByuser)){
            System.out.println(nameByuser.getPassword());
            return "参数为空";
        }
        if (nameByuser.getUsername().equals(username)){
            boolean matches = bCryptPasswordEncoder.matches( password,nameByuser.getPassword());
            if (matches){
//                String token = JwtUtils.generateTokenExpireInMinutes(nameByuser.getId(),60);
                return "登陆成功";
            }else {
                return "登陆失败";
            }

        }else {
            return "账户有误";
        }
    }

    @Override
    public boolean sms(SmsDto dto) {
        String code = RandomUtil.getSixBitRandom();

        String phone = dto.getPhone();
        String host = "https://intlsms.market.alicloudapi.com";
        String path = "/comms/sms/sendmsgall";
        String method = "POST";
        String appcode = "57d436747b684c62a8b00c0fb6ece47a";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("callbackUrl", "http://test.dev.esandcloud.com");
        bodys.put("channel", "0");
        bodys.put("mobile", "+86" + phone);
        bodys.put("templateID", "0000000");
        bodys.put("templateParamSet", code + ",2");


///////////////////////////

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse httpResponse = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(httpResponse.toString());
            System.out.println(code);
            redisTemplate.boundValueOps("code").set(code);
            log.info("添加到Redis成功验证码为:",code);
            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断验证码是否正确
     * @param dto
     * @return
     */

    @Override
    public boolean smslogin(SmsDto dto) {
        String code = dto.getCode();
        String code1 = redisTemplate.boundValueOps("code").get();
        if (code1.equals(code)){
            log.info("验证码判断成功");
            return true;
        }
        return false;
    }


}